package com.aysafety.eventmodel.corn;

import com.aysafety.eventmodel.common.DateUtils;
import com.aysafety.eventmodel.service.integrationWarning.MultiParameterIntegratedWarningService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
@Order(3)
public class MultiParameterIntegratedWarningTask {

    private Logger logger = LoggerFactory.getLogger(MultiParameterIntegratedWarningTask.class);

    @Autowired
    private MultiParameterIntegratedWarningService multiParameterIntegratedWarningService;

    //@Scheduled(cron = "0 0 0 * * *")
    public void multiParamsWarningRealtime() {
        String currentTime = DateUtils.getTime();
        logger.info("正在计算多参量综合预警WcurrentTime..." + currentTime);
        try {
            //microseismService.warning(currentTime);
            multiParameterIntegratedWarningService.multiParameterIntegratedWarning(currentTime);
        } catch (Throwable throwable) {
            logger.error("frequency error,currentTime:" + currentTime);
        }
        logger.info("完成多参量综合预警W计算currentTime..." + currentTime);
    }

    @Scheduled(cron = "30 47 16 5 * ?")
    public void multiParamsWarningHistory() {
        logger.info("正在计算多参量综合预警W...历史");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-05-07 13:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2018-01-01 00:00:00");
                Date endDate = dateFormat1.parse("2019-02-02 16:02:55");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    multiParameterIntegratedWarningService.multiParameterIntegratedWarning(currentTime);
                    c.setTime(date);
                    c.add(Calendar.HOUR_OF_DAY, 1);
                    date = c.getTime();
                    logger.info("程序运行中..." + date.toString());
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
